// pages/hosearch/hosearch.js
Page({
//清除历史记录
cleanhistory: function(e) {
  this.setData({
    history: false, //隐藏历史记录
    historyArray: [], //清空历史记录数组
    newArray: [],
    shoopingtext: "" ,//清空搜索框
    noneview:false
  })
},
//搜索
search: function(e) {
  var searchtext = this.data.shoopingtext; //搜索框的值
  var sss = true;
  if (searchtext != "") {
    //将搜索框的值赋给历史数组
    this.data.historyArray.push(searchtext);
    //模糊查询 循环查询数组中的title字段
    for (var index in this.data.shoopingarray) {
      var num = this.data.shoopingarray[index].title.indexOf(searchtext);
      let temp = 'shoopingarray[' + index + '].status';
      if (num != -1) { //不匹配的不显示
        this.setData({
          [temp]: 1,
        })
        sss = false //隐藏未找到提示
      }
    }
    this.setData({
      history: false, //隐藏历史记录
      noneview: sss, //隐藏未找到提示
      shoppinglist: true, //显示商品列表
      newArray: this.data.historyArray //给新历史记录数组赋值
    })
  } else {
    this.setData({
      noneview: true, //显示未找到提示
      shoppinglist: false, //隐藏商品列表
      history: false, //隐藏历史记录
    })
  }
},
  /**
   * 页面的初始数据
   */
  data: {
    shoopingtext: "", //搜索框的值
    history: false, //显示历史记录
    noneview: false, //显示未找到提示
    shoppinglist: false, //显示商品列表
    historyArray: [], //历史记录数组,
    newArray: [], //添加历史记录数组
  shoopingarray: [
    { //商品
      id: 0,
      images: "http://82.157.94.115:2009/static/images/goods1.PNG",
      title: "智能动感单车 C1 Lite",
      money: "1849.00",
      sold: "1000+",
      status: 0
  },{
    id: 1,
    images: "http://82.157.94.115:2009/static/images/goods2.PNG",
    title: "乳酸菌发酵 果蔬饮",
    money: "71.00",
    sold: "100+",
    status: 0
  },{
    id: 2,
    images: "http://82.157.94.115:2009/static/images/goods3.PNG",
    title: "燃脂派对 男子短裤",
    money: "149.00",
    sold: "100+",
    status: 0
  },{
    id: 3,
    images: "http://82.157.94.115:2009/static/images/goods4.PNG",
    title: "运动蛋白粉 450g/袋",
    money: "99.00",
    sold: "100+",
    status: 0
  },{
    id: 4,
    images: "http://82.157.94.115:2009/static/images/goods5.PNG",
    title: "舒适运动束发发带",
    money: "35.00",
    sold: "100+",
    status: 0
  },{
    id: 5,
    images: "http://82.157.94.115:2009/static/images/goods6.PNG",
    title: "高蛋白纤维巧克力",
    money: "54.00",
    sold: "100+",
    status: 0
  },{
    id: 6,
    images: "http://82.157.94.115:2009/static/images/goods7.PNG",
    title: "燃脂跳绳基础款",
    money: "28.00",
    sold: "100+",
    status: 0
  },{
    id: 7,
    images: "http://82.157.94.115:2009/static/images/goods8.PNG",
    title: "易烊千玺同款智能运动手环B2",
    money: "169.00",
    sold: "100+",
    status: 0
  }
]
},
//搜索框的值
shoppinginput: function(e) {
  //当删除input的值为空时
  if (e.detail.value == "") {
    this.setData({
      history: true, //显示历史记录
      shoppinglist: false //隐藏商品列表
    });
    //所有商品列表的状态改为0
    for (var index in this.data.shoopingarray) {
      let temp = 'shoopingarray[' + index + '].status';
      this.setData({
        [temp]: 0,
      })
    }
  }
  this.setData({
    shoopingtext: e.detail.value
  })
},
//点击历史记录赋值给搜索框
textfz: function(e) {
  this.setData({
    shoopingtext: e.target.dataset.text
  })
},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})